import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:meal/components/finance/balance.dart';
import 'package:meal/constant/fonts.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/tools.dart';

///
/// 余额明细
///
class BalanceDetail extends StatefulWidget {
  const BalanceDetail({Key? key}) : super(key: key);

  @override
  State<BalanceDetail> createState() => _BalanceDetailState();
}

class _BalanceDetailState extends State<BalanceDetail> with SingleTickerProviderStateMixin {
  late TabController _controller;

  List<String> tabs = ['全部', '收入', '支出'];

  PreferredSizeWidget get bottomWidget {
    return PreferredSize(
        preferredSize: const Size.fromHeight(Sizes.default50),
        child: Container(
          color: Colors.white,
          alignment: Alignment.center,
          child: ToolsUtils.getClientTabBar(tabs, _controller),
        ));
  }

  Widget get actionWidget {
    return BrnIconAction(
      iconPressed: () {
        BrnDateRangePicker.showDatePicker(context,
            isDismissible: true,
            minDateTime: DateTime(2019, 01, 01),
            maxDateTime: DateTime(2030, 12, 30),
            pickerMode: BrnDateTimeRangePickerMode.date,
            minuteDivider: 10,
            pickerTitleConfig: const BrnPickerTitleConfig(titleContent: '选择日期范围'),
            dateFormat: 'yyyy-MM-dd',
            initialStartDateTime: DateTime.now(),
            initialEndDateTime: DateTime.now(),
            themeData: BrnPickerConfig(
                titleTextStyle: BrnTextStyle(
                    color: ColorsUtils.parseColorFromHexString(ColorsUtils.color333),
                    fontSize: Sizes.default15,
                    fontWeight: Fonts.w5)), onConfirm: (DateTime startDateTime,
                DateTime endDateTime, List<int> startList, List<int> endList) {
          BrnToast.show('onConfirm: $startDateTime $endDateTime $startList $endList', context);
        });
      },
      size: Sizes.default16,
      child: ImageIcon(ToolsUtils.assetImageIcon('date.png')),
    );
  }

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('余额明细', bottom: bottomWidget, actions: actionWidget),
      body: TabBarView(
        controller: _controller,
        children:
            tabs.asMap().map((key, value) => MapEntry(key, Balance(status: key))).values.toList(),
      ),
    );
  }
}
